﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using GuRenFang.Service.Entity;
using GuRenFang.Service.IBiz;

namespace GuRenFang.Controllers.Management
{
    public class MessageController : Controller
    {
        private readonly IMessagesBiz _messageService;

        public MessageController(IMessagesBiz messageService)
        {
            _messageService = messageService;
        }

        [Authorize]
        public ActionResult List(int? page = 1)
        {
            ViewBag.Model = "Messages";

            short size = Convert.ToInt16(ConfigurationManager.AppSettings["AdminpageSize"]);

            int totalRecord = 0;
            IEnumerable<Messages> news = _messageService.GetItemsByPages(out totalRecord,
                                                                         pageIndex: page.GetValueOrDefault(),
                                                                         pageSize: size, orderFiled: "");

            var item = new PageItem
                           {
                               CurrentPage = page.GetValueOrDefault(),
                               TotalRecords = totalRecord,
                               TotalPages = (totalRecord%size == 0) ? totalRecord/size : totalRecord/size + 1,
                               ControllerName = "Message",
                               ActionName = "List"
                           };

            var viewModels = new Pager<Messages> {DataSource = news, Item = item};

            return View(viewModels);
        }

        [Authorize]
        public ActionResult Delete(int? id = 1)
        {
            _messageService.Delete(id.GetValueOrDefault());
            return RedirectToAction("List");
        }
    }
}
